<?php

class pdd extends bhCommon {


    public function __construct()
    {
        parent::__construct();
    }



    /**
     * pdd列表
     */
    public function category()
    {
        $url = 'http://gw-api.pinduoduo.com/api/router';
        $data = array(
            'type' =>  'pdd.goods.opt.get',
            'client_id' => getConfig('pdd_client_id'),
            'timestamp' => time(),
            'parent_opt_id' => 0
        );
        $data['sign'] = autograph($data);
        $data = json_decode(http_post($url, $data), true);

        $category = $data['goods_opt_get_response']['goods_opt_list'];
        if ($category) {
            foreach ($category as $k => $value) {
                if ($value['opt_name'] == '充值') {
                    unset($category[$k]);
                    break;
                }
            }
        }


        $this->json(array('category' => $category));
    }


    /**
     * 商品列表
     */
    public function goods()
    {
        $opt_id = $this->get('opt_id');
        $keyword = $this->get('pdd_keyword');
        $page = $this->get('page', 1);
        $url = 'http://gw-api.pinduoduo.com/api/router';
        $data = array(
            'type' =>  'pdd.ddk.goods.search',
            'client_id' => getConfig('pdd_client_id'),
            'timestamp' => time(),
            //'opt_id' => $opt_id,
            'page' => $page,
            'page_size' => 10,
            'sort_type' => 0,
            'with_coupon' => 'true'
        );

        if ($keyword) {
            $data['keyword'] = $keyword;
            $check = '催情香水费洛蒙口罩';
            if (stripos($check, $keyword) !== false) {
                $this->json('内容包含敏感信息,修改后再提交', 0);
            }


        } else {
            $data['opt_id'] = $opt_id;
        }

        $data['sign'] = autograph($data);
        $data = json_decode(http_post($url, $data), true);
        $this->json(array('pddGoodsList' => $data['goods_search_response']['goods_list']));
    }


    /**
     * 拼多多详情
     */
    public function detail()
    {
        $goods_id = $this->get('goods_id');

        $pdd_pid = getConfig('pdd_pid');

        $url = 'http://gw-api.pinduoduo.com/api/router';
        $data = array(
            'type' =>  'pdd.ddk.goods.detail',
            'client_id' => getConfig('pdd_client_id'),
            'timestamp' => time(),
            'p_id' => $pdd_pid,
            'goods_id_list' => '[' . $goods_id . ']'
        );
        $data['sign'] = autograph($data);
        $data = json_decode(http_post($url, $data), true);
        $info = $data['goods_detail_response']['goods_details'][0];
        $info['coupon_start_time'] = date('m月d日', $info['coupon_start_time']);
        $info['coupon_end_time'] = date('m月d日', $info['coupon_end_time']);


        $id = $this->get('id');
        $goods = p_get('integral_goods', array('id' => $id));

        $config = array(
            'integral_name' => getConfig('integral_name', '积分')
        );

        $data = array(
            'new_det_list' => $info,
            'goods' => $goods,
            'config' => $config,
        );

        $this->json($data);
    }


    /**
     * 推广链接生成
     */
    public function generate()
    {
        $goods_id = $this->get('goods_id');
        $id = $this->get('id');
        if ($id) {
            $goods = p_get('integral_goods', array('id' => $id));
            if ($goods) {
                try{
                    pdo_begin();
                    if (!$this->bargain->change_integral($this->member['id'], $goods['exchange_number'], 10)) {
                        throw new Exception($this->bargain->error);
                    }
                    $integral_order = array(
                        'goods_id' => $goods['id'],
                        'relation_goods_id' => $goods['relation_goods_id'],
                        'member_id' => $this->member['id'],
                        'exchange_number' => $this->member['exchange_number'],
                        'status' => 3,
                        'type' => 2,
                        'complete_time' => time(),
                        'is_pay' => 2,
                        'created' => time()
                    );
                    if (!p_insert('integral_order', $integral_order)) {
                        throw new Exception('兑换失败');
                    }
                    pdo_commit();
                } catch (Exception $e) {
                    pdo_rollback();
                    $this->json($e->getMessage(), 0);
                }

            }
        }


        $pdd_pid = getConfig('pdd_pid');

        $url = 'http://gw-api.pinduoduo.com/api/router';
        $data = array(
            'type' =>  'pdd.ddk.goods.promotion.url.generate',
            'client_id' => getConfig('pdd_client_id'),
            'timestamp' => time(),
            'p_id' => $pdd_pid,
            'goods_id_list' => '[' . $goods_id . ']',
            'generate_we_app' => 'true',
            'generate_qq_app' => 'true',
            'custom_parameters' => $this->member['id'],
            'generate_authority_url'=>'true'
        );

        $data['sign'] = autograph($data);
        $data = json_decode(http_post($url, $data), true);
        if (isQq()) {
            json(array('qq_app_info' => $data['goods_promotion_url_generate_response']['goods_promotion_url_list'][0]['qq_app_info']));
        } else {
            json(array('we_app_info' => $data['goods_promotion_url_generate_response']['goods_promotion_url_list'][0]['we_app_info']));
        }
    }


    /**
     * 热门推荐
     */
    public function popular()
    {
        $popular_recommend = getConfig('popular_recommend');
        $popular_recommend = $popular_recommend ? json_decode($popular_recommend, true) : '';

        $this->json(array('popular_recommend' => $popular_recommend));
    }

}